exports.singleChat = function (req, res) {

    //获取参数信息
    var bodyParams = req.routeInfo.bodyParams;

    //返回结果
    var resultData = {};

    //查询发送人主键id
    var  u_id     = ""

    //查询接收人id及基本信息
    var  to_id    = ""
    var  todata   = {}

    //命名空间主键id
    var n_id = ""

    //进行最后一条数据的转换 单双引号的转换

    //存储到会话列表的数据
    var newJson = {
        type   : bodyParams.newType,
        content: bodyParams.content.replace(/\s/g, "&nbsp;")
    }

    //存储到会话列表的数据

    var content = JSON.stringify(newJson).replace(/\"/g, "'")

    //存储到聊天记录中的数据
    var conversationContent = bodyParams.content
    //保存聊天记录到聊天记录表
    function chattingecords (hm_sid,hm_receive_id,hm_send_id){
        db.add({
            "hm_sid"           : hm_sid,            //所属的会话ID
            "hm_receive_id"    : hm_receive_id,     //接收人ID
            "hm_send_id"       : hm_send_id,        //发送人ID
            "hm_type"          : bodyParams.newType,           //消息类型 0-文本 1-图片 2-图文 3-语音 4-视频 5-位置 6-文件
            "hm_content"       : conversationContent,        //消息内容
            "hm_create_time"   : common.nowTime(),  //发送时间
            "hm_is_read"       : 0,//是否已读 0-否 1-是
        },"scoket_history_message",(rs)=>{
        })
    }
    //任务流创建
    async.waterfall([

        //查询命名空间id
        //发送单聊消息
        //判断发送人和接收人是否有此会话 给发送人和接收人各自创建一个会话 存储对应会话的聊天记录

        (cb)=> {
            db.field('n_id').where({
                "n_appid" : bodyParams.severAppId
            }).select("socket_namespace",(rs) => {
                //不存在
                if(rs.length == 0){
                    return RES.errorResponse(res,"此appId不存在");
                }

                n_id = rs[0].n_id
                cb();
            })

        },

        (cb) => {

            db.field('s_id').where({
                "s_send_id" : bodyParams.fromUserId,
                "s_receive_id" : bodyParams.toUserId,
                "s_belongs_id" : bodyParams.fromUserId
            }).select("socket_session",(rs)=>{
                if(rs.length != 0){

                    //已经存在此会话
                    db.where({
                        "s_id" : rs[0].s_id,

                    }).update({
                        "s_desc"        : content,
                        "s_create_time" : common.nowTime(),
                    },"socket_session",(rss)=>{
                        resultData = rs[0].s_id
                        cb()
                        chattingecords(rs[0].s_id,bodyParams.toUserId,bodyParams.fromUserId)
                    })
                }else{

                    db.field('s_id').where({
                        "s_send_id" : bodyParams.toUserId,
                        "s_receive_id" : bodyParams.fromUserId,
                        "s_belongs_id" : bodyParams.fromUserId
                    }).select("socket_session",(rs)=>{

                        if(rs.length != 0){

                            //已经存在此会话
                            db.where({
                                "s_id" : rs[0].s_id,

                            }).update({
                                "s_desc"        :content,
                                "s_create_time" : common.nowTime(),
                            },"socket_session",(rss)=>{
                                resultData = rs[0].s_id
                                cb()
                                chattingecords(rs[0].s_id,bodyParams.toUserId,bodyParams.fromUserId)
                            })
                        }else{

                            //不存在此会话
                            db.add({
                                "s_title"       : bodyParams.to_title,
                                "s_desc"        : content,
                                "s_send_id"     : bodyParams.fromUserId,
                                "s_receive_id"  : bodyParams.toUserId,
                                "s_belongs_id"  : bodyParams.fromUserId,
                                "s_type"        : 0,
                                "s_create_time" : common.nowTime(),
                                "s_header"      : bodyParams.header,
                            },"socket_session",(rs)=>{

                                resultData = rs
                                cb()
                                chattingecords(rs,bodyParams.toUserId,bodyParams.fromUserId)
                            })
                        }
                    })
                }
            })
        },
        (cb) => {
            db.field('s_id,s_unread_count').where({
                "s_send_id" : bodyParams.fromUserId,
                "s_receive_id" : bodyParams.toUserId,
                "s_belongs_id" : bodyParams.toUserId
            }).select("socket_session",(rs)=>{

                if(rs.length != 0){

                    //已经存在此会话
                    //更新
                    db.where({
                        "s_id" : rs[0].s_id,

                    }).update({
                        "s_desc"           :content,
                        "s_create_time" : common.nowTime(),
                        "s_unread_count": rs[0].s_unread_count + 1,
                    },"socket_session",(rss)=>{
                        cb()
                        chattingecords(rs[0].s_id,bodyParams.toUserId,bodyParams.fromUserId)

                    })
                }else{

                    db.field('s_id,s_unread_count').where({
                        "s_send_id" : bodyParams.toUserId,
                        "s_receive_id" : bodyParams.fromUserId,
                        "s_belongs_id" : bodyParams.toUserId
                    }).select("socket_session",(rs)=>{

                        if(rs.length != 0){
                            db.where({
                                "s_id" : rs[0].s_id,

                            }).update({
                                "s_desc"        : content,
                                "s_create_time" : common.nowTime(),
                                "s_unread_count": rs[0].s_unread_count + 1,
                            },"socket_session",(rss)=>{
                                cb()
                                chattingecords(rs[0].s_id,bodyParams.toUserId,bodyParams.fromUserId)

                            })
                        }else{

                            //不存在此会话
                            db.add({
                                "s_title"       : bodyParams.form_title,
                                "s_desc"        : content,
                                "s_send_id"     : bodyParams.fromUserId,
                                "s_receive_id"  : bodyParams.toUserId,
                                "s_belongs_id"  : bodyParams.toUserId,
                                "s_type"        : 0,
                                "s_create_time" : common.nowTime(),
                                "s_unread_count": 1,
                                "s_header"      : bodyParams.header,
                            },"socket_session",(rs)=>{
                                cb()
                                chattingecords(rs,bodyParams.toUserId,bodyParams.fromUserId)

                            })
                        }
                    })
                }
            })
        },
        (cb) => {

            todata = bodyParams
            todata.content = conversationContent
            todata.messageType = 'singleChat'//发送私聊消息
            todata.newType = bodyParams.newType//发送消息类型
            socketObj._privateChat(todata,bodyParams.toUserId,(isTrue) => {

                cb()

            })
        }

    ],()=> {

        //返回最终拼接结果
        RES.successResponse(res,resultData);

    });
    
}